package com.kch.system.service.impl;

import com.kch.system.domain.SysBusiness;
import com.kch.system.domain.SysUser;
import com.kch.system.domain.UserCollect;
import com.kch.system.mapper.UserCollectMapper;
import com.kch.system.req.UserCollectBrowseReq;
import com.kch.system.service.IUserCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

@Service
public class UserCollectServiceImpl implements IUserCollectService {

    @Autowired
    private UserCollectMapper userCollectMapper;

    @Override
    public int insertUserCollect(UserCollectBrowseReq req, SysUser sysUser) {
        UserCollect collect = userCollectMapper.selectByUserAndBus(sysUser.getUserId().intValue(), req.getBusId(), req.getCloumnCode());
        if(ObjectUtils.isEmpty(collect)){
            UserCollect userCollect = new UserCollect();
            userCollect.setBusId(req.getBusId());
            userCollect.setCloumnCode(req.getCloumnCode());
            userCollect.setUserId(sysUser.getUserId().intValue());
            userCollect.setCreateBy(sysUser.getLoginName());
            userCollect.setCreateTime(new Date());
            return userCollectMapper.insert(userCollect);
        }else{
            return userCollectMapper.deleteByUserAndBus(sysUser.getUserId().intValue(), req.getBusId(), req.getCloumnCode());
        }
    }

    @Override
    public List<SysBusiness> selectCollectPage(SysUser sysUser) {
        return userCollectMapper.selectCollectByUserId(sysUser.getUserId().intValue());
    }
}
